home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / Source / HippoDraw / HippoDrawSrc1.1 / Hippo.subproj / Ruler.m < prev    next >
Encoding:
Text File  |  1992-04-25  |  6.0 KB  |  243 lines

  1. #import "Ruler.h"
  2. #import <appkit/Font.h>
  3. #import <appkit/Text.h>
  4. #import <dpsclient/wraps.h>
  5. #import <math.h>
  6.  
  7. #define LINE_X (15.0)
  8. #define WHOLE_HT (10.0)
  9. #define HALF_HT (8.0)
  10. #define QUARTER_HT (4.0)
  11. #define EIGHTH_HT (2.0)
  12. #define NUM_X (3.0)
  13.  
  14. #define WHOLE (72)
  15. #define HALF (WHOLE/2)
  16. #define QUARTER (WHOLE/4)
  17. #define EIGHTH (WHOLE/8)
  18.  
  19. @implementation Ruler
  20.  
  21. + (NXCoord)width
  22. {
  23.     return 23.0;
  24. }
  25.  
  26. - initFrame:(const NXRect *)frameRect
  27. {
  28.     [super initFrame:frameRect];
  29.     [self setFont:[Font newFont:NXSystemFont size:8.0 matrix:NX_IDENTITYMATRIX]];
  30.     startX = [[self class] width];
  31.     return self;
  32. }
  33.  
  34. - setFlipped:(BOOL)flag
  35. /*
  36.  * This view doesn't work when it's "flipped," so we
  37.  * recycle that idea to mean whether the ruler has "0" at
  38.  * the origin of the view or not.
  39.  */
  40. {
  41.     flipped = flag ? YES : NO;
  42.     return self;
  43. }
  44.  
  45. - setFont:aFont
  46. {
  47.     NXCoord as, lh;
  48.  
  49.     font = aFont;
  50.     NXTextFontInfo(aFont, &as, &descender, &lh);
  51.     if (descender < 0.0) descender = -1.0 * descender;
  52.  
  53.     return self;
  54. }
  55.  
  56. - drawHorizontal:(const NXRect *)rects
  57. {
  58.     NXRect line, clip;
  59.     int curPos, last, mod, i, j;
  60.  
  61.     PSsetgray(NX_LTGRAY);
  62.     NXRectFill(rects);
  63.  
  64.     if (lastlp >= rects->origin.x && lastlp < rects->origin.x + rects->size.width) lastlp = -1.0;
  65.     if (lasthp >= rects->origin.x && lasthp < rects->origin.x + rects->size.width) lasthp = -1.0;
  66.  
  67.     line = bounds;                /* draw bottom line */
  68.     line.size.height = 1.0;
  69.     PSsetgray(NX_DKGRAY);
  70.     if (NXIntersectionRect(rects, &line)) NXRectFill(&line);
  71.  
  72.     line = bounds;
  73.     line.size.width = 1.0;
  74.     line.origin.x = startX - 1.0;
  75.     if (NXIntersectionRect(rects, &line)) NXRectFill(&line);
  76.  
  77.     line = bounds;                /* draw ruler line */
  78.     line.origin.y = LINE_X;
  79.     line.size.height = 1.0;
  80.     line.origin.x = startX;
  81.     line.size.width = bounds.size.width - startX;
  82.  
  83.     PSsetgray(NX_BLACK);
  84.     if (NXIntersectionRect(rects, &line)) NXRectFill(&line);
  85.  
  86.     clip = *rects;
  87.     clip.origin.x = startX;
  88.     clip.size.width = bounds.size.width - startX;
  89.     if (NXIntersectionRect(rects, &clip)) {
  90.     curPos = (int)(NX_X(&clip) - startX);
  91.     last = (int)(NX_MAXX(&clip) - startX);
  92.     if (mod = (curPos % EIGHTH)) curPos -= mod;
  93.     if (mod = (last % EIGHTH)) last -= mod;
  94.     line.size.width = 1.0;
  95.     [font set];
  96.     for (j = curPos; j <= last; j += EIGHTH) {
  97.         i = flipped ? bounds.size.width - j : j;
  98.         line.origin.x =  startX + (float)i - (flipped ? 1.0 : 0.0);
  99.         if (!(i % WHOLE)) {
  100.         char buf[10];
  101.         line.origin.y = LINE_X - WHOLE_HT;
  102.         line.size.height = WHOLE_HT;
  103.         NXRectFill(&line);
  104.         PSmoveto(((float) j + NUM_X) + startX, descender + line.origin.y - 2.0);
  105.         sprintf(buf, "%d", i / WHOLE);
  106.         PSshow(buf);
  107.         } else if (!(i % HALF)) {
  108.         line.origin.y = LINE_X - HALF_HT;
  109.         line.size.height = HALF_HT;
  110.         NXRectFill(&line);
  111.         } else if (!(i % QUARTER)) {
  112.         line.origin.y = LINE_X - QUARTER_HT;
  113.         line.size.height = QUARTER_HT;
  114.         NXRectFill(&line);
  115.         } else if (!(i % EIGHTH)) {
  116.         line.origin.y = LINE_X - EIGHTH_HT;
  117.         line.size.height = EIGHTH_HT;
  118.         NXRectFill(&line);
  119.         }
  120.     }
  121.     }
  122.  
  123.     return self;
  124. }
  125.  
  126.  
  127. - drawVertical:(const NXRect *)rects
  128. {
  129.     NXRect line, clip;
  130.     int curPos, last, mod, i, j;
  131.  
  132.     PSsetgray(NX_LTGRAY);
  133.     NXRectFill(rects);
  134.  
  135.     if (lastlp >= rects->origin.y && lastlp < rects->origin.y + rects->size.height) lastlp = -1.0;
  136.     if (lasthp >= rects->origin.y && lasthp < rects->origin.y + rects->size.height) lasthp = -1.0;
  137.  
  138.     line = bounds;                /* draw bottom line */
  139.     line.origin.x = bounds.size.width - 1.0;
  140.     line.size.width = 1.0;
  141.     PSsetgray(NX_DKGRAY);
  142.     if (NXIntersectionRect(rects, &line)) NXRectFill(&line);
  143.  
  144.     line = bounds;                /* draw ruler line */
  145.     line.origin.x = bounds.size.width - LINE_X - 2.0;
  146.     line.size.width = 1.0;
  147.     PSsetgray(NX_BLACK);
  148.     if (NXIntersectionRect(rects, &line)) NXRectFill(&line);
  149.  
  150.     clip = *rects;
  151.     line.origin.x++;
  152.     if (NXIntersectionRect(rects, &clip)) {
  153.     curPos = (int)(NX_Y(&clip));
  154.     last = (int)(NX_MAXY(&clip));
  155.     if (mod = (curPos % EIGHTH)) curPos -= mod;
  156.     if (mod = (last % EIGHTH)) last -= mod;
  157.     line.size.height = 1.0;
  158.     [font set];
  159.     for (j = curPos; j <= last; j += EIGHTH) {
  160.         i = flipped ? bounds.size.height - j : j;
  161.         line.origin.y = (float)j - (flipped ? 1.0 : 0.0);
  162.         if (!(i % WHOLE)) {
  163.         char buf[10];
  164.         line.size.width = WHOLE_HT;
  165.         NXRectFill(&line);
  166.         PSmoveto(line.origin.x + 5.0, (float)j + (flipped ? - 10.0 : 2.0));
  167.         sprintf(buf, "%d", i / WHOLE);
  168.         PSshow(buf);
  169.         } else if (!(i % HALF)) {
  170.         line.size.width = HALF_HT;
  171.         NXRectFill(&line);
  172.         } else if (!(i % QUARTER)) {
  173.         line.size.width = QUARTER_HT;
  174.         NXRectFill(&line);
  175.         } else if (!(i % EIGHTH)) {
  176.         line.size.width = EIGHTH_HT;
  177.         NXRectFill(&line);
  178.         }
  179.     }
  180.     }
  181.  
  182.     return self;
  183. }
  184.  
  185. - drawSelf:(const NXRect *) rects :(int)rectCount
  186. {
  187.     if (frame.size.width < frame.size.height) {
  188.     [self drawVertical:rects];
  189.     } else {
  190.     [self drawHorizontal:rects];
  191.     }
  192.     return self;
  193. }
  194.  
  195.  
  196. #define SETPOSITION(value) (isVertical ? (rect.origin.y = value - (absolute ? 0.0 : 1.0)) : (rect.origin.x = value + (absolute ? 0.0 : startX)))
  197. #define SETSIZE(value) (isVertical ? (rect.size.height = value) : (rect.size.width = value))
  198. #define SIZE (isVertical ? rect.size.height : rect.size.width)
  199.  
  200. - doShowPosition:(NXCoord)lp :(NXCoord)hp absolute:(BOOL)absolute
  201. {
  202.     NXRect rect;
  203.     BOOL isVertical = (frame.size.width < frame.size.height);
  204.  
  205.     rect = bounds;
  206.  
  207.     if (!absolute && !isVertical) {
  208.     if (lp < 0.0) lp -= startX;
  209.     if (hp < 0.0) hp -= startX;
  210.     }
  211.  
  212.     SETSIZE(1.0);
  213.     lastlp = SETPOSITION(lp);
  214.     NXHighlightRect(&rect);
  215.     lasthp = SETPOSITION(hp);
  216.     NXHighlightRect(&rect);
  217.  
  218.     return self;
  219. }
  220.  
  221. - showPosition:(NXCoord)lp :(NXCoord)hp
  222. {
  223.     [self lockFocus];
  224.     if (notHidden) [self doShowPosition:lastlp :lasthp absolute:YES];
  225.     [self doShowPosition:lp :hp absolute:NO];
  226.     [self unlockFocus];
  227.     notHidden = YES;
  228.     return self;
  229. }
  230.  
  231. - hidePosition
  232. {
  233.     if (notHidden) {
  234.     [self lockFocus];
  235.     [self doShowPosition:lastlp :lasthp absolute:YES];
  236.     [self unlockFocus];
  237.     notHidden = NO;
  238.     }
  239.     return self;
  240. }
  241.  
  242. @end
  243.